#/****************************************************************************
# Copyright (c) 2013-2014 cocos2d-x.org
#
# http://www.cocos2d-x.org
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ****************************************************************************/
cmake_policy(SET CMP0017 NEW)

cmake_minimum_required(VERSION 2.8)

set(APP_NAME airbattledemo)
project (${APP_NAME})

set(COCOS2D_ROOT ${CMAKE_SOURCE_DIR}/cocos2d)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${COCOS2D_ROOT}/cmake/Modules/")
include(CocosBuildHelpers)

# libcocos2d
set(BUILD_CPP_TESTS OFF CACHE BOOL "turn off build cpp-tests")
set(BUILD_LUA_LIBS OFF CACHE BOOL "turn off build lua related targets")
set(BUILD_JS_LIBS OFF CACHE BOOL "turn off build js related targets")
add_subdirectory(${COCOS2D_ROOT})

# Some macro definitions
if(WINDOWS)
  if(BUILD_SHARED_LIBS)
    ADD_DEFINITIONS (-D_USRDLL -D_EXPORT_DLL_ -D_USEGUIDLL -D_USREXDLL -D_USRSTUDIODLL)
  else()
    ADD_DEFINITIONS (-DCC_STATIC)
  endif()

  ADD_DEFINITIONS (-DCOCOS2DXWIN32_EXPORTS -D_WINDOWS -DWIN32 -D_WIN32)
  set(PLATFORM_FOLDER win32)
elseif(MACOSX OR APPLE)
  ADD_DEFINITIONS (-DCC_TARGET_OS_MAC)
  ADD_DEFINITIONS (-DUSE_FILE32API)
  set(PLATFORM_FOLDER mac)
elseif(LINUX)
  ADD_DEFINITIONS(-DLINUX)
  set(PLATFORM_FOLDER linux)
elseif(ANDROID)
  ADD_DEFINITIONS (-DUSE_FILE32API)
  set(PLATFORM_FOLDER android)
else()
  message( FATAL_ERROR "Unsupported platform, CMake will exit" )
endif()


# Compiler options
if(MSVC)
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS
                  -wd4251 -wd4244 -wd4334 -wd4005 -wd4820 -wd4710
                  -wd4514 -wd4056 -wd4996 -wd4099)
else()
  set(CMAKE_C_FLAGS_DEBUG "-g -Wall -DCOCOS2D_DEBUG=1")
  set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-deprecated-declarations -Wno-reorder")
  if(CLANG)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
  endif()
endif(MSVC)



set(PLATFORM_SPECIFIC_SRC)
set(PLATFORM_SPECIFIC_HEADERS)
if(MACOSX OR APPLE)
set(PLATFORM_SPECIFIC_SRC
  proj.ios_mac/ios/main.m
  proj.ios_mac/ios/RootViewController.mm
  proj.ios_mac/ios/AppController.mm
)
set(PLATFORM_SPECIFIC_HEADERS
  proj.ios_mac/ios/RootViewController.h
  proj.ios_mac/ios/AppController.h
)
elseif(LINUX) #assume linux
set(PLATFORM_SPECIFIC_SRC
  proj.linux/main.cpp
)
elseif ( WIN32 )
set(PLATFORM_SPECIFIC_SRC
  proj.win32/main.cpp
)
set(PLATFORM_SPECIFIC_HEADERS
  proj.win32/main.h
  proj.win32/resource.h
)
endif()

include_directories(
  /usr/local/include/GLFW
  /usr/include/GLFW
  ${COCOS2D_ROOT}/cocos
  ${COCOS2D_ROOT}/cocos/platform
  ${COCOS2D_ROOT}/cocos/audio/include/
  ${CMAKE_SOURCE_DIR}/Classes
  ${CMAKE_SOURCE_DIR}/../../inc
)
if ( WIN32 )
  include_directories(
  ${COCOS2D_ROOT}/external/glfw3/include/win32
  ${COCOS2D_ROOT}/external/win32-specific/gles/include/OGLES
)
endif( WIN32 )

file(GLOB_RECURSE GAME_SRC  "${CMAKE_SOURCE_DIR}/Classes/*.cpp")
file(GLOB_RECURSE GAME_HEADERS  "${CMAKE_SOURCE_DIR}/Classes/*.h")

set(GAME_SRC
  ${GAME_SRC}
  ${PLATFORM_SPECIFIC_SRC}
)

set(GAME_HEADERS
  ${GAME_HEADERS}
  ${PLATFORM_SPECIFIC_HEADERS}
)

# mark_as_advanced(CMAKE_BUILD_TYPE)
IF( NOT CMAKE_BUILD_TYPE )
  # SET( CMAKE_BUILD_TYPE "Debug" )
  SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Debug or Release")
  SET_PROPERTY(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release) 
ENDIF()

set (COMPILER_LIB_EXT "lib")
IF(MSVC)
  set (COMPILE_POSTFIX "msvc")
  set (COMPILER_LIB_EXT "lib")
ELSEIF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
  set (COMPILE_POSTFIX "gcc")
  set (COMPILER_LIB_EXT "a")
ENDIF()

set (BEHAVIAC_LIB_POSTFIX ${COMPILE_POSTFIX}_${CMAKE_BUILD_TYPE})
set( CMAKE_DEBUG_POSTFIX "_${COMPILE_POSTFIX}_debug" )
set( CMAKE_RELEASE_POSTFIX "_${COMPILE_POSTFIX}_release" )

string( TOLOWER ${BEHAVIAC_LIB_POSTFIX} BEHAVIAC_LIB_POSTFIX )

if(GAME_HEADERS)
  if ( WIN32 )
    add_executable(${APP_NAME} WIN32 ${GAME_SRC} ${GAME_HEADERS})
  else()
    add_executable(${APP_NAME} ${GAME_SRC} ${GAME_HEADERS})
  endif ( WIN32 )
else()
  if ( WIN32 )
    add_executable(${APP_NAME} WIN32 ${GAME_SRC})
  else()
    add_executable(${APP_NAME} ${GAME_SRC})
  endif ( WIN32 )
endif()
set (COMPILE_POSTFIX "")


set (BEHAVIAC_LIB_PATH "${CMAKE_SOURCE_DIR}/../../lib")
message (STATUS "BEHAVIAC_LIB_PATH=${BEHAVIAC_LIB_PATH}")

set (BEHAVIAC_DLL_PATH "${CMAKE_SOURCE_DIR}/../../bin")

set (BEHAVIAC_LIB_NAME "libbehaviac_${BEHAVIAC_LIB_POSTFIX}")
message (STATUS "BEHAVIAC_LIB_NAME=${BEHAVIAC_LIB_NAME}")

set (BEHAVIAC_LIB ${BEHAVIAC_LIB_PATH}/${BEHAVIAC_LIB_NAME}.${COMPILER_LIB_EXT})
# find_library(BEHAVIAC_LIB 
#   NAMES ${BEHAVIAC_LIB_NAME}
#   PATHS ${BEHAVIAC_LIB_PATH}
#   )

target_link_libraries(${APP_NAME} cocos2d ${BEHAVIAC_LIB})

set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin")

set_target_properties(${APP_NAME} PROPERTIES
     RUNTIME_OUTPUT_DIRECTORY  "${APP_BIN_DIR}")

if ( WIN32 )
  #also copying dlls to binary directory for the executable to run
  pre_build(${APP_NAME}
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}/Resources
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${COCOS2D_ROOT}/external/win32-specific/gles/prebuilt/glew32.dll ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}/
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${COCOS2D_ROOT}/external/win32-specific/zlib/prebuilt/zlib1.dll ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}/
    )

  add_custom_command(TARGET ${APP_NAME} POST_BUILD     
      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${BEHAVIAC_DLL_PATH}/${BEHAVIAC_LIB_NAME}.dll  ${APP_BIN_DIR}/Resources
      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}/${APP_NAME}.exe  ${APP_BIN_DIR}/Resources
      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}/glew32.dll  ${APP_BIN_DIR}/Resources
      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}/zlib1.dll  ${APP_BIN_DIR}/Resources
      )

  # copy from Resources to bin
  add_custom_command(TARGET ${APP_NAME} POST_BUILD     
      COMMAND ${CMAKE_COMMAND} -E remove_directory ${BEHAVIAC_DLL_PATH}/airbattledemo
      COMMAND ${CMAKE_COMMAND} -E copy_directory ${APP_BIN_DIR}/Resources  ${BEHAVIAC_DLL_PATH}/airbattledemo
    )
  
else()
  pre_build(${APP_NAME}
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}/Resources
    )

endif()
